package org.cnam.mobileShopping.bean;

import org.cnam.mobileShopping.wsclient.WSEntityListId;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Classe représentant un produit
 * 
 * @author laurent
 * 
 */
@WSEntityListId("product")
public class Product implements Comparable<Product> {
	private Integer productid;

	private String description;

	private String barcode;

	private Integer categoryid;

	@Override
	public String toString() {
		return this.description;
	}

	public Product() {

	}

	public Product(Integer productid, String description, String barcode,
			Integer categoryid) {
		super();
		this.productid = productid;
		this.description = description;
		this.barcode = barcode;
		this.categoryid = categoryid;
	}

	@Override
	public int hashCode() {
		int hash = 0;
		hash += (productid != null ? productid.hashCode() : 0);
		return hash;
	}

	@Override
	public boolean equals(Object object) {
		// TODO: Warning - this method won't work in the case the id fields are
		// not set
		if (!(object instanceof Product)) {
			return false;
		}
		Product other = (Product) object;
		if ((this.productid == null && other.productid != null)
				|| (this.productid != null && !this.productid
						.equals(other.productid))) {
			return false;
		}
		return true;
	}

	public Integer getProductid() {
		return productid;
	}

	public void setProductid(Integer productid) {
		this.productid = productid;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getBarcode() {
		return barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public Integer getCategoryid() {
		return categoryid;
	}

	public void setCategoryid(Integer categoryid) {
		this.categoryid = categoryid;
	}

	public static Product parseJSON(JSONObject jsonObject) throws JSONException {
		return new Product(jsonObject.getInt("id"),
				jsonObject.getString("description"),
				jsonObject.getString("barcode"),
				jsonObject.getInt("categoryId"));
	}

	@Override
	public int compareTo(Product another) {
		// TODO Auto-generated method stub
		return this.getDescription().compareToIgnoreCase(
				another.getDescription());
	}

}
